//
//  NSDate+Extension.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/19.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import Foundation
import UIKit

extension NSDate{
    
    class func stringFormTime(createdTime:String) ->String {
        
        //把字符串转会时间
        let formatter = NSDateFormatter()
        //Mon Oct 19 20:22:46 +0800 2015
        formatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        formatter.locale = NSLocale(localeIdentifier: "en_US")
        
        let time = formatter.dateFromString(createdTime)!
        
        //判断是不是今年
        
        if self.isYear(time){
            //判断是不是今天
            if self.isToday(time){
                
                let nowDate = NSDate()
                let timeInterval = nowDate.timeIntervalSinceDate(time)
                if timeInterval < 60{
                    return "刚刚"
                }else if timeInterval < 60*60{
                    return "\(Int(timeInterval / 60))分钟前"
                }else {
                    return "\(Int(timeInterval / (60*60)))小时前"
                }
                
            }else  if self.isYeasToday(time){
                //判断是不是昨天
                formatter.dateFormat = "HH:mm"
                return "昨天 \(formatter.stringFromDate(time))"
            }else{
                //如果是今年
                formatter.dateFormat = "MM-dd HH:mm"
                return formatter.stringFromDate(time)
            }
        }else{
            //如果不是今年返回样式
            formatter.dateFormat = "yyyy-MM-dd HH:mm"
            return formatter.stringFromDate(time)
        }
        
    }
    
    //判断是不是今年
    private class func isYear(createdTime:NSDate) -> Bool{
        
        let formatter = NSDateFormatter()
        //Mon Oct 19 20:22:46 +0800 2015
        formatter.dateFormat = "yyyy"
        
        let createdStr = formatter.stringFromDate(createdTime)
        
        let nowStr = formatter.stringFromDate(NSDate())
        
        return (createdStr as NSString).isEqualToString(nowStr)
    }
    
    //判断是不是今天
    private class func isToday(createdTime:NSDate) -> Bool{
        
        let formatter = NSDateFormatter()
        //Mon Oct 19 20:22:46 +0800 2015
        formatter.dateFormat = "yyyy-MM-dd"
        
        let createdStr = formatter.stringFromDate(createdTime)
        
        let nowStr = formatter.stringFromDate(NSDate())
        
        return (createdStr as NSString).isEqualToString(nowStr)
        
    }
    
    //判断是不是昨天
    private class func isYeasToday(var createdTime:NSDate) -> Bool{
        
        //取出当前时间
        var nowDate = NSDate()
        
        let formatter = NSDateFormatter()
        //Mon Oct 19 20:22:46 +0800 2015
        formatter.dateFormat = "yyyy-MM-dd"
        //把时间转换成字符串
        let createdStr = formatter.stringFromDate(createdTime)
        let nowStr = formatter.stringFromDate(nowDate)
        //再将字符串转换成时间
        createdTime = formatter.dateFromString(createdStr)!
        nowDate = formatter.dateFromString(nowStr)!
        
        let timeInterval = nowDate.timeIntervalSinceDate(createdTime)
        
        if timeInterval == 24 * 60 * 60{
            return true
        }
        return false
    }
}